﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using System.Globalization;

namespace BL
{
   public class Utiltty
    {
        public int ConvertToInt(object obj, int defaultValue)
        {
            try
            {
                return Convert.ToInt32(obj);
            }
            catch (Exception)
            {

                return defaultValue;
            }

        }
        public int ConvertToInt(object obj)
        {
            return Convert.ToInt32(obj);
        }
        public double ConvertToDouble(object obj, double defaultValue)
        {
            try
            {
                return Convert.ToDouble(obj);
            }
            catch (Exception)
            {

                return defaultValue;
            }

        }
        public double ConvertToDouble(object obj)
        {
            return Convert.ToDouble(obj);
        }
        public decimal ConvertToDecimal(object obj, decimal defaultValue)
        {
            try
            {
                return Convert.ToDecimal(obj);
            }
            catch (Exception)
            {

                return defaultValue;
            }

        }

        public decimal ConvertToDecimal(object obj)
        {
            return Convert.ToDecimal((obj==null||obj.ToString()==""?0:obj));
        }
        public string SetDecimalFormat(object d)
        {
            if (d != null)
            {
                return string.Format("{0:#,0.00}", d);
            }
            else
            {
                return "0.00";
            }
        }
        public string SetDateFormat(object date)
        {
            string dateFormat = string.Empty;
            if (date != null)
            {
                try
                {
                    DateTime d = DateTime.Parse(date.ToString());
                    dateFormat = d.ToString("dd-MMM-yyyy");
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return dateFormat;
        }
        public  DateTime? ConvertStringToDate(string DisplayDate)
        {
            DateTime? returnDate = null;
            if (!string.IsNullOrEmpty(DisplayDate))
            {
                DateTime date = DateTime.MinValue;
                string CultureSymbol = "en-us";
                if (DateTime.TryParseExact(DisplayDate, "yyyy-MM-dd HH:mm:ss", new CultureInfo(CultureSymbol), DateTimeStyles.None, out date))
                    returnDate = date;
            }

            return returnDate;
        }
      
    }
}
